package com.dataset.manager.controller;

import com.dataset.manager.model.Dataset;
import com.dataset.manager.service.DatasetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/datasets")
@Api(tags = "数据集管理")
public class DatasetController {
    
    @Autowired
    private DatasetService datasetService;

    @Data
    public static class DatasetRequest {
        private String name;
        private String description;
        // 可以根据前端实际发送的字段添加更多属性
        private String sourceType;
        private String sourcePath;
        private Object processingOptions;
        private String filePattern;
    }

    @PostMapping
    @ApiOperation("创建数据集")
    public ResponseEntity<Dataset> createDataset(@RequestBody DatasetRequest request) {
        try {
            System.out.println("收到创建数据集请求: " + request);

            // 检查必需参数
            if (request.name == null || request.name.trim().isEmpty()) {
                System.out.println("名称参数为空或无效");
                return ResponseEntity.badRequest().build();
            }

            // 启动异步创建任务，传递数据源参数
            datasetService.createDataset(request.name, request.description, request.sourceType, request.sourcePath);
            
            // 获取所有数据集并找到最新创建的那个（刚刚创建的）
            List<Dataset> datasets = datasetService.listDatasets();
            if (datasets.isEmpty()) {
                System.out.println("数据集列表为空，创建失败");
                return ResponseEntity.badRequest().build();
            }
            
            // 假设最新添加的数据集是我们刚刚创建的
            Dataset latestDataset = datasets.get(datasets.size() - 1);
            System.out.println("成功创建数据集: " + latestDataset);
            // 返回刚刚创建的数据集对象
            return ResponseEntity.ok(latestDataset);
        } catch (Exception e) {
            System.err.println("创建数据集时发生异常: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }
    
    @DeleteMapping("/{id}")
    @ApiOperation("删除数据集")
    public ResponseEntity<String> deleteDataset(@PathVariable String id) {
        boolean deleted = datasetService.deleteDataset(id);
        if (deleted) {
            return ResponseEntity.ok("数据集删除成功");
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping
    @ApiOperation("列出所有数据集")
    public ResponseEntity<List<Dataset>> listDatasets() {
        List<Dataset> datasets = datasetService.listDatasets();
        return ResponseEntity.ok(datasets);
    }
    
    @GetMapping("/{id}")
    @ApiOperation("获取特定数据集")
    public ResponseEntity<Dataset> getDataset(@PathVariable String id) {
        Dataset dataset = datasetService.getDatasetById(id);
        if (dataset != null) {
            return ResponseEntity.ok(dataset);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}